import datetime
import time
from apscheduler.events import EVENT_ALL, EVENT_JOB_ERROR, EVENT_JOB_EXECUTED, JobSubmissionEvent
from logzero import logger
from apscheduler.schedulers.background import BackgroundScheduler
from apscheduler.executors.pool import ThreadPoolExecutor, ProcessPoolExecutor
from tornado.ioloop import IOLoop
from apscheduler.events import JobEvent, JobExecutionEvent, JobSubmissionEvent
import logging

logging.basicConfig()
logging.getLogger('apscheduler').setLevel(logging.DEBUG)
executors = {
        'default': ThreadPoolExecutor(20),
        'processpool': ProcessPoolExecutor(5)
    }
    #创建调度器：BlockingScheduler
scheduler = BackgroundScheduler(executors=executors)

def func():
    logger.info('do func2 time：')

def func2(index: int):
    logger.info('do func%s time：', index)
   # time.sleep(2)

def callback_func2(event: JobExecutionEvent):
    if(event.exception):
        logger.info(event.traceback)
    #logger.info('do func WELL %s', event.code)

def dojob():
    #添加任务,时间间隔2S
    #scheduler.add_job(func, 'interval', seconds=2, id='test_job1')
    #添加任务,时间间隔5S
    
    scheduler.start()

if __name__ == "__main__":
    dojob()
    for i in range(0, 100):
        scheduler.add_job(func2, 'interval', seconds=2, id='test_job' + str(i), args=([i,]))
        scheduler.add_listener(callback_func2, mask=EVENT_JOB_EXECUTED | EVENT_JOB_ERROR)
        scheduler.get_job("")
    try:
        IOLoop.current().start()
    except KeyboardInterrupt:
        IOLoop.current().stop()
        logger.info("Interrupt catched")